$(function () {
    obj.init();
});
var obj = new function () {
    'use strict';
    return {
        data:null,
        local:null,
        showName:false,
        init:function () {
            this.funObj().removeJsCssFile('bootstrap.min.css', 'css');//和百度地图样式冲突，移除
            this.funObj().removeJsCssFile('simplify.css', 'css');
            this.funObj().addInfo();
            // this.funObj().showMap();
            this.listener();
        },
        listener:function () {
            var $this = this;
            //省份更改
            $('#province').change(function() {
                $this.funObj().changeProv($(this).val());
            }).trigger('change');
            //市更改
            $('#city').change(function() {
                $this.funObj().changeCity($(this).val());
            }).trigger('change');
            $("#checkShowName").on("click", function(){
                if ($(this).is(":checked")) {
                    $this.showName = true;
                } else {
                    $this.showName = false;
                }
                var provId = $('#province').val();
                var cityId = $('#city').val();
                if (cityId) {
                    $this.funObj().getMapData({provId:provId, cityId:cityId});
                } else {
                    $this.funObj().getMapData({provId:provId});
                }
            });
        },
        funObj:function () {
            var $this = this;
            return {
                addInfo: function() {
                    $('#province').select2({placeholder: '请选择'});
                    $('#city').select2({placeholder: '请选择'})
                },
                showMap: function (_adds, local, level) {
                    var map = new BMap.Map("l-map");
                    map.centerAndZoom(new BMap.Point(local.lng, local.lat), level);//展示的城市地标，缩放等级
                    map.enableScrollWheelZoom(true);
                    var myGeo = new BMap.Geocoder();
                    $.each(_adds, function (index_key, value) {
                        geocodeSearch(value, index_key);
                    });
                    function geocodeSearch(add, index){
                        myGeo.getPoint(add, function(point){
                            if (point) {
                                var address = new BMap.Point(point.lng, point.lat);
                                addMarker(address,new BMap.Label(index,{offset:new BMap.Size(20,-10)}), index);
                            }
                        },local);
                    }
                    // 编写自定义函数,创建标注
                    function addMarker(point,label,title){
                        var marker = new BMap.Marker(point);
                        map.addOverlay(marker);
                        if ($this.showName) {
                            marker.setLabel(label);
                        }
                        marker.setTitle(title);//鼠标移上去显示学校
                    }
                },
                removeJsCssFile: function(filename, filetype){
                    var targetElement = (filetype === "js") ? "script" : (filetype === "css") ? "link" : "none";
                    var targetAttr = (filetype === "js") ? "src" : (filetype === "css") ? "href" : "none";
                    var allSuspects = document.getElementsByTagName(targetElement);
                    for (var i = allSuspects.length; i >= 0; i--) {
                        if (allSuspects[i] && allSuspects[i].getAttribute(targetAttr) != null && allSuspects[i].getAttribute(targetAttr).indexOf(filename) != -1) {
                            allSuspects[i].parentNode.removeChild(allSuspects[i]);
                        }
                    }
                },
                changeProv: function (provId) {
                    var cityPro = $('#city');
                    cityPro.html('');
                    if (provId !== '全国') {
                        cityPro.attr("disabled", false);
                    } else {
                        cityPro.attr("disabled", true);
                    }
                    xhExtra.ajaxRequest('device/getCityByProv', {provId:provId}, {async:false}, function (result) {
                        cityPro.select2({placeholder: '请选择', data: result, allowClear: true});
                    });
                    $this.funObj().getMapData({provId:provId});
                },
                changeCity: function (cityId) {
                    var provId = $('#province').val();
                    if (cityId) {
                        $this.funObj().getMapData({provId:provId, cityId:cityId});
                    } else {
                        $this.funObj().getMapData({provId:provId});
                    }
                },
                getMapData: function (_params) {
                    xhExtra.ajaxRequest('device/getMapDataByProv', _params, {async:false}, function (result) {
                        var geoCoder = new BMap.Geocoder();
                        geoCoder.getPoint(result.local, function(local_point){
                            $this.funObj().showMap(result.data, local_point, result.level);
                        },result.local);
                    });
                }
            };
        }
    }
};